package work.jame.blog.configuration.web;

import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
import work.jame.blog.configuration.filter.RequestLimitInterceptor;

import java.time.format.DateTimeFormatter;

/**
 * 跨域配置类
 *
 * @author : Jame
 * @date : 2023-05-10 15:18
 **/
@Configuration
public class WebConfig implements WebMvcConfigurer {


    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //配置允许跨域的路径
        registry.addMapping("/**")
                //允许跨域请求的域名
                .allowedOriginPatterns("*")
                //允许跨域请求类型
                .allowedMethods("GET", "POST")
                //允许cookie
                .allowCredentials(true)
                //允许的请求头属性
                .allowedHeaders("*")
                //跨域时间
                .maxAge(3600);
    }


    /**
     * 添加请求限流拦截器
     *
     * @param registry 拦截注册类
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new RequestLimitInterceptor());
    }

    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }

}
